package com.itheima.demo05JDBC事务_重点;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Demo05 {
	public static void main(String[] args) throws SQLException {
        // 1.注册驱动(自动注册)
        // 2.获取连接
        Connection connection = null;
        Statement statement = null;
        try {
         connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql_day2", "root", "root");

        // 3.开启事务
        connection.setAutoCommit(false);
        // 4.获取到Statement
        statement = connection.createStatement();
        // 5.Statement执行SQL

            // 张三-500
            statement.executeUpdate("update user SET balance = balance - 500 where username = 'zhangsan'");
            // 李四+500
            statement.executeUpdate("update user SET balance = balance + 500 where username = 'lisi'");
            connection.commit();
        }catch (Exception e){
            connection.rollback();
        }

        connection.close();
        statement.close();
        // 6.成功提交事务

        // 6.失败回滚事务
    }
}
